﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxAddTeacher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetSubject()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Subject> lstSub = DataBase.GetAllSubject(schId);
                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        if (s.NotNeedTeacher < 1)
                        {
                            JSubject js = AssignHelp.SubToJSub(s);
                            lstJSub.Add(js);
                        }
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSub);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetTeacher()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    List<Teacher> lstTeach = DataBase.GetAllTeacher(schId);
                    List<JTeacher> lstJTeach = new List<JTeacher>();
                    foreach (Teacher s in lstTeach)
                    {
                        JTeacher js = AssignHelp.TeacherTpJTeacher(s);
                        lstJTeach.Add(js);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJTeach);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetTeacherInfo(int teacherId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    Teacher t = DataBase.GetTeacherById(teacherId);
                    List<Subject> lstSub = DataBase.GetSubjectOfTeacher(t);
                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        JSubject js = AssignHelp.SubToJSub(s);
                        lstJSub.Add(js);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSub);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }


        }

        [WebMethod(true)]
        public static string AddTeacher(JTeacher jt, List<JTeacherDetail> details)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //
                    //Lấy dữ liệu
                    Teacher t = AssignHelp.JTeacherToTeacher(jt);
                    t.ScheduleInListId = schId;
                    List<TeacherDetail> lstDT = new List<TeacherDetail>();
                    foreach (JTeacherDetail jtd in details)
                    {
                        TeacherDetail td = AssignHelp.JTeacherDToTeacherD(jtd);
                        lstDT.Add(td);
                    }
                    try
                    {
                        DataBase.InsertTeacher(t, lstDT);
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                    return GUIConstants.SUCCESS_RESULT;
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }

        [WebMethod(true)]
        public static string UpdateTeacher(int teacherId, JTeacher jt, List<JTeacherDetail> jdetails)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    Teacher t = AssignHelp.JTeacherToTeacher(jt);
                    t.ScheduleInListId = schId;
                    List<TeacherDetail> lstDT = new List<TeacherDetail>();
                    foreach (JTeacherDetail jtd in jdetails)
                    {
                        TeacherDetail td = AssignHelp.JTeacherDToTeacherD(jtd);
                        lstDT.Add(td);
                    }
                    try
                    {
                        DataBase.UpdateTeacher(teacherId, t, lstDT);
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                    return GUIConstants.SUCCESS_RESULT;
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string DeleteTeacher(List<int> lstId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    //Lấy dữ liệu
                    try
                    {
                        DataBase.DeleteTeacher(lstId);
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                    return GUIConstants.SUCCESS_RESULT;
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
         
        }
    }
}
